package com.example.airoles.controller;

import com.example.airoles.dto.UserProfile;
import com.example.airoles.service.UserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserProfileController {
    
    @Autowired
    private UserProfileService userProfileService;
    
    @PostMapping("/profile")
    public ResponseEntity<UserProfile> createProfile(@RequestBody Map<String, String> request) {
        String userId = request.get("userId");
        String nickname = request.get("nickname");
        String email = request.get("email");
        
        if (userId == null || userId.trim().isEmpty()) {
            return ResponseEntity.badRequest().build();
        }
        
        UserProfile profile = userProfileService.createProfile(userId, nickname, email);
        return ResponseEntity.ok(profile);
    }
    
    @GetMapping("/profile/{userId}")
    public ResponseEntity<UserProfile> getProfile(@PathVariable String userId) {
        UserProfile profile = userProfileService.getProfile(userId);
        if (profile == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(profile);
    }
    
    @PutMapping("/profile/{userId}")
    public ResponseEntity<UserProfile> updateProfile(
            @PathVariable String userId, 
            @RequestBody UserProfile updatedProfile) {
        UserProfile profile = userProfileService.updateProfile(userId, updatedProfile);
        if (profile == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(profile);
    }
    
    @GetMapping("/preferences/{userId}")
    public ResponseEntity<UserProfile.UserPreferences> getPreferences(@PathVariable String userId) {
        UserProfile.UserPreferences preferences = userProfileService.getPreferences(userId);
        return ResponseEntity.ok(preferences);
    }
    
    @PutMapping("/preferences/{userId}")
    public ResponseEntity<UserProfile.UserPreferences> updatePreferences(
            @PathVariable String userId, 
            @RequestBody UserProfile.UserPreferences preferences) {
        UserProfile.UserPreferences updated = userProfileService.updatePreferences(userId, preferences);
        return ResponseEntity.ok(updated);
    }
    
    @PostMapping("/favorites/{userId}/{roleId}")
    public ResponseEntity<Void> addFavoriteRole(
            @PathVariable String userId, 
            @PathVariable String roleId) {
        userProfileService.addFavoriteRole(userId, roleId);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/favorites/{userId}/{roleId}")
    public ResponseEntity<Void> removeFavoriteRole(
            @PathVariable String userId, 
            @PathVariable String roleId) {
        userProfileService.removeFavoriteRole(userId, roleId);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/favorites/{userId}")
    public ResponseEntity<List<String>> getFavoriteRoles(@PathVariable String userId) {
        List<String> favorites = userProfileService.getFavoriteRoles(userId);
        return ResponseEntity.ok(favorites);
    }
    
    @GetMapping("/analytics/{userId}")
    public ResponseEntity<Map<String, Object>> getUserAnalytics(@PathVariable String userId) {
        Map<String, Object> analytics = userProfileService.getUserAnalytics(userId);
        if (analytics.isEmpty()) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(analytics);
    }
    
    @GetMapping("/recommendations/{userId}")
    public ResponseEntity<List<String>> getRecommendations(@PathVariable String userId) {
        List<String> recommendations = userProfileService.getRecommendationsForUser(userId);
        return ResponseEntity.ok(recommendations);
    }
    
    @DeleteMapping("/profile/{userId}")
    public ResponseEntity<Void> deleteProfile(@PathVariable String userId) {
        boolean deleted = userProfileService.deleteProfile(userId);
        if (deleted) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    @GetMapping("/stats/system")
    public ResponseEntity<Map<String, Object>> getSystemStats() {
        Map<String, Object> stats = userProfileService.getSystemStats();
        return ResponseEntity.ok(stats);
    }
    
    @GetMapping("/active")
    public ResponseEntity<List<UserProfile>> getActiveUsers(
            @RequestParam(defaultValue = "10") int limit) {
        List<UserProfile> activeUsers = userProfileService.getActiveUsers(limit);
        return ResponseEntity.ok(activeUsers);
    }
}